package com.mrtop.jigsawapp;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class Levels extends Activity {

	private Button btnlevel1;
	private List<Button> buttonList = new ArrayList<Button>();
	private List<Boolean> isLooked = new ArrayList<Boolean>();
	private List<Integer> listImg = new ArrayList<Integer>();
	private final int numOfLevels = 6;
	private List<Integer> listMan = new ArrayList<Integer>();
	private List<ImageView> listImgView = new ArrayList<ImageView>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.levels);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Button btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), Main.class);
				finish();
				startActivity(intent);
			}
		});
		loadData();
		getLevel();

		btnlevel1 = (Button) findViewById(R.id.btnL1);
		btnlevel1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), Game.class);
				intent.putExtra("LEVEL", "1:240");
				finish();
				startActivity(intent);
			}
		});
	}

	public void getLevel() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			listMan.add(Integer.parseInt(extras.getString("B1")));
			listMan.add(Integer.parseInt(extras.getString("B2")));
			listMan.add(Integer.parseInt(extras.getString("B3")));
			listMan.add(Integer.parseInt(extras.getString("B4")));
			listMan.add(Integer.parseInt(extras.getString("B5")));
			listMan.add(Integer.parseInt(extras.getString("B6")));

			for (int i = 1; i < listMan.size(); i++) {
				if (listMan.get(i) != -1) {
					isLooked.set(0, true);
					boolean flag = false;
					switch (i) {
					case 0:
						
						break;
					case 1:
						break;
					case 2:
						break;
					case 3:
						break;
					}
				}
			}
			setLock();
		}
	}

	public void loadData() {
		buttonList.add((Button) findViewById(R.id.btnL1));
		buttonList.add((Button) findViewById(R.id.btnL2));
		buttonList.add((Button) findViewById(R.id.btnL3));
		buttonList.add((Button) findViewById(R.id.btnL4));
		buttonList.add((Button) findViewById(R.id.btnL5));
		buttonList.add((Button) findViewById(R.id.btnL6));
		
		listImgView.add((ImageView) findViewById(R.id.ImgView1));
		listImgView.add((ImageView) findViewById(R.id.ImgView2));
		listImgView.add((ImageView) findViewById(R.id.ImgView3));
		listImgView.add((ImageView) findViewById(R.id.ImgView4));
		listImgView.add((ImageView) findViewById(R.id.ImgView5));
		listImgView.add((ImageView) findViewById(R.id.ImgView6));

		for (int i = 0; i < numOfLevels; i++) {
			isLooked.add(false);
		}
		isLooked.set(0, true);

		listImg.add(R.drawable.man1_win);
		listImg.add(R.drawable.man2_win);
		listImg.add(R.drawable.man3_win);
		listImg.add(R.drawable.man4_win);
		listImg.add(R.drawable.man5_win);
		listImg.add(R.drawable.man6_win);

		buttonList.get(0).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(0, "1:250");
			}
		});
		buttonList.get(1).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(1, "2:200");
			}
		});
		buttonList.get(2).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(2, "3:150");
			}
		});
		buttonList.get(3).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(3, "4:100");
			}
		});
		buttonList.get(4).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(4, "5:50");
			}
		});
		buttonList.get(5).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnLxClicked(5, "6:30");
			}
		});
	}

	public void btnLxClicked(int i, String level) {
		if (isLooked.get(i)) {
			Intent intent = new Intent(getBaseContext(), Game.class);
			intent.putExtra("LEVEL", level);
			finish();
			startActivity(intent);
		} else {
			Toast.makeText(getApplicationContext(), "Unlocked!!!",
					Toast.LENGTH_SHORT).show();
		}

	}

	public void setLock() {
		for (int i = 0; i < isLooked.size(); i++) {
			if (isLooked.get(i)) {
				Button tmp = buttonList.get(i);
				tmp.setBackgroundResource(listImg.get(i));
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.levels, menu);
		return true;
	}

}
